package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.User;
import cn.sunlelin.educationserver.param.LoginParam;
import cn.sunlelin.educationserver.service.UserService;
import cn.sunlelin.educationserver.util.JWTUtil;
import cn.sunlelin.educationserver.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//注解  ssm
@RestController
@CrossOrigin  //跨域
public class UserController {
    @Autowired
    private UserService userService;

    /*
    * POST请求   http://localhost:8089/user/createUser
    * 创建用户
    * */
    @PostMapping("/user/createUser")
    public Map<String, Object> createUser(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        if(userService.createUser(user)){
            map.put("code", 200);
            map.put("msg", "创建用户成功");
            map.put("dataobject", null);
        }
        else {
            map.put("code", 500);
            map.put("msg", "创建用户失败，该用户名已被使用");
            map.put("dataobject", null);
        }
        return map;
    }

    /*
    * Post请求 http://localhost:8089/user/updateUser
    * 修改用户信息
    * */

    @PostMapping("/user/updateUser")
    public Map<String,Object> updateUser(@RequestBody User user){
        Map<String, Object> map = new HashMap<>();
        if(userService.updateUser(user)){
            map.put("code", 200);
            map.put("msg", "修改用户成功");
            map.put("dataobject", null);
        }
        else {
            map.put("code", 500);
            map.put("msg", "修改用户失败，该用户名已被使用");
            map.put("dataobject", null);
        }
        return map;
    }

    //Post请求 http://localhost:8089/user/updatePassword
    @PostMapping("/user/updatePassword")
    public Map<String,Object> updatePassword(@RequestBody User user){
        Map<String, Object> map = new HashMap<>();
        if(userService.updatePassword(user)){
            map.put("code", 200);
            map.put("msg", "修改用户密码成功");
            map.put("dataobject", null);
        }
        else {
            map.put("code", 500);
            map.put("msg", "修改用户失败，该用户不存在");
            map.put("dataobject", null);
        }
        return map;
    }

    @PostMapping("/user/loginByUserName")
    public Map<String,Object> loginByUserName(@RequestBody User user){
        User u=userService.loginByUserName(user);
        Map<String,Object> map=new HashMap<>();
        if (u!=null) {
            Map<String,String> payload=new HashMap<>();
            payload.put("userId",u.getUserId().toString());
            payload.put("userName",u.getUserName());
            String token= JWTUtil.createToken(payload);

            map.put("code", 200);
            map.put("msg", "用户登录成功");
            map.put("token", token);
            map.put("userId", u.getUserId());
            map.put("userName", u.getUserName());
            map.put("avatar",u.getAvatar());
        } else {
            map.put("code", 404);
            map.put("msg", "用户名或密码错误");
            map.put("token", null);
            map.put("userId", null);
            map.put("userName", null);
            map.put("avatar",null);
        }
        return map;
    }

    // 登录
    @PostMapping("/user/loginByEmail")
    public R login(@RequestBody LoginParam loginParam) {
        return userService.login(loginParam);
    }

    // 注册
    @PostMapping("/user/registerByEmail")
    public R register(@RequestBody LoginParam loginParam) {
        return userService.register(loginParam);
    }

    // 找回密码
    @PostMapping("/user/findPassword")
    public R findPassword(@RequestBody LoginParam loginParam) {
        return userService.findPassword(loginParam);
    }
    // 修改邮箱
    @PostMapping("/user/updateEmail")
    public R updateEmail(@RequestBody LoginParam loginParam) {
        return userService.updateEmail(loginParam);
    }

    @GetMapping("/admin/user/findAll")
    public Map<String,Object> findAll(){
        List<User> userList=userService.findAll();
        Map<String,Object> map=new HashMap<>();
        if (userList!=null) {
            map.put("code", 200);
            map.put("msg", "查询所有用户成功");
            map.put("dataobject", userList);
        } else {
            map.put("code", 500);
            map.put("msg", "查询所有用户失败");
            map.put("dataobject", null);
        }
        return map;
    }

    //Get请求   http://localhost:8089/user/selectUserAllById/{userId}
    @GetMapping("/user/selectUserAllById/{userId}")
    public Map<String, Object> selectUserAllById(@PathVariable Integer userId) {
        User user = userService.selectUserAllById(userId);
        Map<String, Object> map = new HashMap<>();
        if (user != null) {
            map.put("code", 200);
            map.put("msg", "根据userId查找用户成功");
            map.put("dataobject", user);
        } else {
            map.put("code", 500);
            map.put("msg", "根据userId查找用户失败");
            map.put("dataobject", null);
        }
        return map;
    }

    //Get请求   http://localhost:8089/user/deleteUser/{userId}
    @GetMapping("/user/deleteUser/{userId}")
    public Map<String, Object> deleteUser(@PathVariable Integer userId) {
        Map<String, Object> map = new HashMap<>();
        if(userService.deleteUser(userId)){
            map.put("code", 200);
            map.put("msg", "删除用户成功");
            map.put("dataobject", null);
        }
        else {
            map.put("code", 500);
            map.put("msg", "删除用户失败，该用户不存在");
            map.put("dataobject", null);
        }
        return map;
    }


}
